$(function() {
    // 展示资源树
	$("#tt").tree({
		url:path+'/resource/tree',
		animate:true,
		lines:true,
		onClick:function(node){
			$("#select_all_btn").removeAttr("checked");
			$("#resource_id").val(node.id);
			if(node.id == 0){ // 说明是父级节点
				createDiv([]);
				return;
			}
			var url = path + "/roleResource/sign/" + $("#role_id").val() + "-" + node.id;
			$.post(url,function(data){
				createDiv(data);
			});
		}				
	});
});

function saveM(){
	if($.common.isEmpty($("#resource_id").val())){
		$.alert.error("请先选择资源！");
		return;
	}
	$.form.submit({
		url: path + "/roleResource/save",
		success: function(result){
            // 返回有失败信息
            if (result.state == web_status.SUCCESS) {
            	$.alert.success("分配成功！");
            }else{
            	$.alert.error(result.msg || "操作失败！");
            }
        }
	});
}

// 辅助js
function createDiv(data){
	if(data.length == 0){
		$("#saveBtn").attr("disabled","disabled");
	}else{
		$("#saveBtn").removeAttr("disabled");
	}
	var div = "";
	$.each(data,function(index,value){
		if(value.checked){
			div += "<div class=\"col-xs-2\"><div>"
				 +"<label for='signbox_"+value.id+"'>"
				 +"<input checked id='signbox_"+value.id+"' name=\"signs\" value='"+value.sign_name+"' type=\"checkbox\" class=\"ace\">"
				 +"<span class=\"lbl\">"+value.sign_remark+"</span></label></div></div>";
		}else{
			div += "<div class=\"col-xs-2\"><div>"
				 +"<label for='signbox_"+value.id+"'>"
				 +"<input id='signbox_"+value.id+"' name=\"signs\" value='"+value.sign_name+"' type=\"checkbox\" class=\"ace\">"
				 +"<span class=\"lbl\">"+value.sign_remark+"</span></label></div></div>";
		}
	});
	$("#auth_signs_container").empty().append(div);
}

// 返回到角色管理界面
function backM(){
	window.location.href = path + "/role";
}

// 全选和反选
function selectAllM(me){
	if(me.checked){
		$("input[name='signs']").prop("checked",true);
	}else{
		$("input[name='signs']").removeAttr("checked");
	}
}